using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Configuration;
using System.Collections;
using PrintDataGrid;
using System.Collections.Specialized;

namespace eBirdAttend
{
    public partial class LeaveRpt : UserControl
    {

        public string strCon = Properties.Settings.Default["ConString"].ToString();
        SqlConnection sqlCon = new SqlConnection();
        SqlCommand sqlCmd = new SqlCommand();
        DataGridViewPrinter MyDataGridViewPrinter;

        public LeaveRpt()
        {
            InitializeComponent();
            sqlCon.ConnectionString = strCon;
            sqlCmd.Connection = sqlCon;
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            try
            {
                string strWhere = "";
                string strStartDate = dateStartDate.Value.Date.ToString("MM/dd/yyyy");
                string strEndDate = dateEndDate.Value.Date.ToString("MM/dd/yyyy");
                //MessageBox.Show(strStartDate);

                int dateStatus = -1;
                if (cmbRange.SelectedIndex == 0)
                {
                    MessageBox.Show("Select the date condtion");
                    cmbRange.Focus();
                    return;
                }
                else
                {
                    if (cmbRange.SelectedIndex == 1)
                    {
                        dateStatus = 2; //between
                        strWhere = "AND AttnDate >= '" + strStartDate.ToString() + "' and AttnDate<='" + strEndDate.ToString() + "'";
                    }
                    else if (cmbRange.SelectedIndex == 2)
                    {
                        dateStatus = 3; //less than equal to
                        strWhere = "AND AttnDate <= '" + strStartDate.ToString() + "' ";
                    }
                    else if (cmbRange.SelectedIndex == 3)
                    {
                        dateStatus = 4; //greater than equal to
                        strWhere = "AND AttnDate >= '" + strStartDate.ToString() + "' ";
                    }
                }
                if (cmbBranch.SelectedIndex != 0)
                {
                    strWhere = strWhere + " and EmpBrID = " + cmbBranch.SelectedValue.ToString();
                }

                if (cmbDept.SelectedIndex != 0)
                {
                    strWhere = strWhere + " and EmpDeptID = " + cmbDept.SelectedValue.ToString();

                }
                //MessageBox.Show(strWhere.ToString());
                if(sqlCon.State == ConnectionState.Closed)
                sqlCon.Open();
                sqlCmd.CommandText = "SELECT     EmpFirstName + ' ' + EmpLastName AS Name, ShName AS Shift, AttnDate AS Date,Description AS DayStatus,"
                          + " DepName AS Department, BrName AS Branch, LtName as LeaveType, LRNote as Description "
                          + " FROM LeaveReportView where Present = 2 ";

                if (strWhere != "")
                {
                    //strWhere = "where " + strWhere;
                    sqlCmd.CommandText += strWhere;
                }
                //MessageBox.Show(sqlCmd.CommandText);
                sqlCmd.CommandType = CommandType.Text;
                sqlCmd.Parameters.Clear();

                sqlCmd.ExecuteNonQuery();
                DataSet ds = new DataSet();
                SqlDataAdapter sqlAdap = new SqlDataAdapter(sqlCmd);
                sqlAdap.Fill(ds);
                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = ds.Tables[0].ToString();
                dataGridView1.Columns[0].MinimumWidth = 140;
                dataGridView1.Columns[1].MinimumWidth = 70;
                dataGridView1.Columns[2].MinimumWidth = 100;
                dataGridView1.Columns[7].MinimumWidth = 150;
                sqlCon.Close();
            }
            catch (Exception ex)
            {
            }
        }

        private void btnShowCrystalReport_Click(object sender, EventArgs e)
        {
            //ShowCrsytalReport _ShowCrsytalReport = new ShowCrsytalReport();
            //_ShowCrsytalReport.strReportName = "Atnd";
            //_ShowCrsytalReport.Show();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void LeaveRpt_Load(object sender, EventArgs e)
        {
           try
            {
                cmbGroup.SelectedIndex = 0;
                departmentDataset.Clear();
                departmentTableAdapter1.Fill(departmentDataset.Department);

                DataTable table1 = new DataTable("Table1");

                table1 = departmentDataset.Tables[0].Clone();

                // Add some rows.
                DataRow row;
                row = table1.NewRow();
                row["depid"] = 0;
                row["depname"] = "[Select]";
                row["depcmpid"] = "1";
                row["dephod"] = "Select";
                table1.Rows.Add(row);

                // Accept changes.
                table1.AcceptChanges();

                departmentDataset.Tables[0].Constraints.Clear();
                DataTable table2 = departmentDataset.Tables[0];

                table1.Merge(table2);

                cmbDept.DataSource = table1;
                cmbDept.DisplayMember = "depname";
                cmbDept.ValueMember = "depid";

                branchTableAdapter1.Fill(branchDataset.Branch);
                table1 = new DataTable("Table1");

                table1 = branchDataset.Tables[0].Clone();

                // Add some rows.
                row = table1.NewRow();
                row["brbranchid"] = 0;
                row["brcmpid"] = 0;
                row["brname"] = "[Select]";

                table1.Rows.Add(row);

                // Accept changes.
                table1.AcceptChanges();

                branchDataset.Tables[0].Constraints.Clear();
                table2 = branchDataset.Tables[0];
                table1.Merge(table2);

                cmbBranch.DataSource = table1;
                cmbBranch.DisplayMember = "brname";
                cmbBranch.ValueMember = "brbranchid";

                cmbRange.SelectedIndex = 0;
                cmbDept.SelectedIndex = 0;
                cmbBranch.SelectedIndex = 0;

                cmbBranch.Focus();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PrintDGV printdgv = new PrintDGV();
            printdgv.Print_DataGridView(dataGridView1);
        }

        private void export_Click(object sender, EventArgs e)
        {
            try
            {
                int j = 1;
                int i;
                string strReportTemplate = Application.StartupPath.ToString() + "\\" +
                     Properties.Settings.Default.PresenceReport.ToString();
                if (strReportTemplate == "")
                {
                    MessageBox.Show("Report Template Name Missing");
                    return;
                }

                //ConfigurationManager.AppSettings["AttendanceReport"].ToString();
                // ConfigurationSettings.AppSettings["AttendanceReport"].ToString();

                StringCollection strHeaderCol = new StringCollection();
                EbExcelAuto exelAttnReport = new EbExcelAuto();
                string curColText = "";
                exelAttnReport.Init();
                exelAttnReport.OpenExcelFile(strReportTemplate);
                exelAttnReport.FillExcellSheetFromDatagrid("#START", dataGridView1, "Date");
                exelAttnReport.SaveAs(null);
                exelAttnReport.Close();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message.ToString());
            }           

        }
        
       

    }
}
